package ThreadPool.jike.demo04;

/**
 * @author oyz
 * 用一把锁来控制转账操作，原子性问题，解决中间状态对外不可见,不可变对象不能当作锁
 */
public class Account1 {

    private Object lock;

    private int balance;


    public Account1(Object lock) {
        this.lock = lock;
    }

    // 转账 对象锁
    public void transfer(Account1 target,int amount){
        synchronized (lock){
            this.balance -= amount;
            target.balance += amount;

        }
    }
    // 类锁
    public void transfer2(Account1 target,int amount){
        synchronized (Account1.class){
            this.balance -= amount;
            target.balance += amount;

        }
    }

}
